package prassein.rsm.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Utility;
import prassein.rsm.dao.base.PermLevelDao;
import prassein.rsm.dto.PermLevelDto;
import prassein.rsm.entity.base.PermLevel;
import prassein.rsm.entity.base.User;
import prassein.rsm.service.base.UserManager;

import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="userCreate.jsp"), 
	@Result(name = "error",location="userCreate.jsp"),
	@Result(name = "input",location="userCreate.jsp")})
public class UserCreateAction extends ActionSupport {

	private static final long serialVersionUID = -2935344190875328133L;

	@Autowired
	private UserManager userManager;
	
	@Autowired
	private PermLevelDao permLevelDao;
	
	private PermLevelDto permLevel;
	
	private User user;
	
	private int isCreateUser;
	
	private int result;
	
	private int userId = -1;
	
	@Override 
	public String execute() throws Exception {
		if (isCreateUser == 1) {
			if (user != null && user.getUserName() != null && !user.getUserName().equals("")) {
				User userTemp = userManager.getUser(user.getUserName());
				if (userTemp != null && user.getId() == null && userTemp.getUserName() != null && userTemp.getUserName().equals(user.getUserName())) {
					result = 2;
				} else {
					User tmp = null;
					if (user.getId() == null) {
						tmp = new User();
					} else {
						tmp = userManager.getUser(user.getId());
					}
					tmp.setPassword(user.getPassword());
					tmp.setUserName(user.getUserName());
					
					int permId = CreatePerm(permLevel);
					tmp.setPermLevel(permId);
					
					userManager.save(tmp);
					
					result = 1;
				}
			}
			return INPUT;
		} else if (userId > 0) {
			user = userManager.getUser(userId);
			permLevel = Utility.GetPermLevelDto(permLevelDao.get(user.getPermLevel()).getResumePerm());
		}
		return INPUT;
	}
	
	private Integer CreatePerm(PermLevelDto permLevel2) {
		PermLevel perm = new PermLevel();
		perm.setResumePerm(permLevel2.getPerm());
		permLevelDao.save(perm);
		return perm.getId();
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getIsCreateUser() {
		return isCreateUser;
	}
	public void setIsCreateUser(int isCreateUser) {
		this.isCreateUser = isCreateUser;
	}
	public int getResult() {
		return result;
	}
	public void setResult(int result) {
		this.result = result;
	}
	public PermLevelDto getPermLevel() {
		return permLevel;
	}
	public void setPermLevel(PermLevelDto permLevel) {
		this.permLevel = permLevel;
	}

	public PermLevelDao getPermLevelDao() {
		return permLevelDao;
	}

	public void setPermLevelDao(PermLevelDao permLevelDao) {
		this.permLevelDao = permLevelDao;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
}
